package com.javaee.term.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaee.term.Enums.SendProductStatusEnum;
import com.javaee.term.constant.StockConstant;
import com.javaee.term.dto.ContactDetailDto;
import com.javaee.term.dto.SendProductDetailDto;
import com.javaee.term.exception.HandException;
import com.javaee.term.form.SendProductForm;
import com.javaee.term.mapper.SendProductListMapper;
import com.javaee.term.model.*;
import com.javaee.term.service.LogistInfoService;
import com.javaee.term.service.ProductService;
import com.javaee.term.service.PurchaseInfoService;
import com.javaee.term.service.SendProductService;
import com.javaee.term.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * author 陈伯瑞
 * date 2019/12/20 0020 11:21
 * version 1.0
 **/
@Controller
@RequestMapping("/seller/sendProduct")
@Slf4j
public class SendProductController {
    @Autowired
    SendProductService sendProductService;
    @Autowired
    ProductService productService;
    @Autowired
    PurchaseInfoService purchaseInfoService;
    //发货列表
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {
        /* 插件实现分页*/
        PageHelper.startPage(page, size);
        List<SendProductList> list = sendProductService.findAll();
        PageInfo<SendProductList> sendInfoPage = new PageInfo<SendProductList>(list);
        map.put("sendInfoPage", sendInfoPage);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("totalpage", sendInfoPage.getPages());
        return new ModelAndView("sendProduct/list", map);
    }
    //商品详情
    @GetMapping("/queryProduct")
    public ModelAndView product(@RequestParam(value = "productId") String productId,
                             Map<String, Object> map) {
        Product product = productService.findOne(productId);
        map.put("product",product);
        return new ModelAndView("sendProduct/product", map);
    }
    //发货单细节
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "sendId") String sendId,
                             Map<String, Object> map){
        SendProductDetailDto sendDetail = sendProductService.findSendDetail(sendId);
        map.put("sendDetailDto",sendDetail);
        map.put("stockmin", StockConstant.STOCKMIN);
        return new ModelAndView("sendProduct/detail", map);
    }
   //确认发货单
    @GetMapping("/saveSend")
    @Transactional
    public ModelAndView saveSend(@RequestParam(value = "sendId") String sendId,
                               Map<String, Object> map){
        SendProductDetailDto sendDetail = sendProductService.findSendDetail(sendId);
        try {
            //判断发货数目和库存关系
            if((sendDetail.getProductStock()-sendDetail.getProductQuantity())<StockConstant.STOCKMIN)
                                   throw new HandException(0,"发货数量和库存不对应");

                SendProductList one = sendProductService.findOne(sendId);
                if(one.getSendProductStatusEnum().getCode()==1)
                    throw new HandException(0,"改货单已确认发货，操作异常");
                //跟新发货单状态和商品的库存
                 sendProductService.sureSendAndUpdateStock(sendId);
        } catch (HandException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/sendProduct/detail?sendId="+sendId);
            return new ModelAndView("common/error", map);
        }
        map.put("msg", "请完善该单物流信息");
        map.put("url", "/sell/seller/logist/index?sendId="+sendId);
        return new ModelAndView("common/success", map);
    }
    //添加修改进货单
    @GetMapping("/purchase")
    public ModelAndView purchase(@RequestParam(value = "productQuantity",required=false) String productQuantity,
                                        @RequestParam(value = "productId",required=false) String productId,
                                 @RequestParam(value = "purchaseId",required=false) String purchaseId,
                                 Map<String, Object> map){
        map.put("productQuantity", productQuantity);
        map.put("productId", productId);
        List<Product> products = productService.findAll();
        map.put("products", products);
        map.put("purchaseId",purchaseId);
        return new ModelAndView("sendProduct/indexPurchase", map);
    }
    //保存进货单
    @PostMapping("/savePurchase")
    @Transactional
    public ModelAndView savePurchase(@Valid  PurchaseInfo purchaseInfo,
                                 Map<String, Object> map){
    try {
        PurchaseInfo purchaseInfo1 = purchaseInfoService.findOne(purchaseInfo.getPurchaseId());
        if(purchaseInfo1==null) {
            purchaseInfo.setCreateTime(new Date());
            purchaseInfo.setPurchaseId(KeyUtil.genUniqueKey());
            purchaseInfoService.insert(purchaseInfo);
        }
        else{
            purchaseInfo1.setProductQuantity(purchaseInfo.getProductQuantity());
            purchaseInfo1.setPurchaseSource(purchaseInfo.getPurchaseSource());
            purchaseInfo1.setProductId(purchaseInfo.getProductId());
            purchaseInfoService.update(purchaseInfo1);
        }
    }catch (HandException e) {
        map.put("msg", e.getMessage());
        map.put("url", "/sell/seller/sendProduct/purchase");
        return new ModelAndView("common/error", map);
    }
        map.put("url", "/sell/seller/sendProduct/listPurchase");
        return new ModelAndView("common/success", map);
    }
    //进货单列表
    @GetMapping("/listPurchase")
    public ModelAndView listPurchase(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {
        /* 插件实现分页*/
        PageHelper.startPage(page, size);
        List<PurchaseInfo> list = purchaseInfoService.findAll();
        PageInfo<PurchaseInfo> purchaseInfoPageInfo = new PageInfo<PurchaseInfo>(list);
        map.put("purchaseInfoPageInfo", purchaseInfoPageInfo);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("totalpage", purchaseInfoPageInfo.getPages());
        return new ModelAndView("sendProduct/listPurchase", map);
    }
    //确认进货单
    @GetMapping("/sureInProduct")
    public ModelAndView purchase(@RequestParam(value = "purchaseId",required=false) String purchaseId,
                                 Map<String, Object> map){
        PurchaseInfo info = purchaseInfoService.findOne(purchaseId);
        try {

            if(info.getFinshStatus()==1)
                throw new HandException(0,"进货单已确认进货，操作异常");
            //跟新进货单状态和商品的库存
           purchaseInfoService.surePurchaseAndUpdateStock(purchaseId);
        } catch (HandException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/sendProduct/listPurchase");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/sell/seller/sendProduct/listPurchase");
        return new ModelAndView("common/success", map);
    }


}
